package com.zyj.check.service.impl;

import java.util.*;

import com.zyj.assets.domain.AssetsInfo;
import com.zyj.check.domain.AssetsCheck;
import com.zyj.check.domain.AssetsCheckDetail;
import com.zyj.check.mapper.AssetsCheckMapper;
import com.zyj.check.service.IAssetsCheckService;
import com.zyj.common.core.domain.entity.SysDept;
import com.zyj.common.core.text.Convert;
import com.zyj.common.utils.DateUtils;
import com.zyj.common.utils.StringUtils;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.service.IAssetsReceiveService;
import com.zyj.repairs.domain.AssetsRepairs;
import com.zyj.repairs.service.IAssetsRepairsService;
import com.zyj.system.mapper.SysDeptMapper;
import com.zyj.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * 资产盘点Service业务层处理
 * 
 * @author zhuyujie
 * @date 2022-05-17
 */
@Service
public class AssetsCheckServiceImpl implements IAssetsCheckService
{
    @Autowired
    private AssetsCheckMapper assetsCheckMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private IAssetsReceiveService assetsReceiveService;

    @Autowired
    private IAssetsRepairsService assetsRepairsService;

    @Autowired
    private ISysUserService sysUserService;




    /**
     * 查询资产盘点
     * 
     * @param checkId 资产盘点主键
     * @return 资产盘点
     */
    @Override
    public AssetsCheck selectAssetsCheckByCheckId(Long checkId)
    {
        return assetsCheckMapper.selectAssetsCheckByCheckId(checkId);
    }

    /**
     * 查询资产盘点列表
     * 
     * @param assetsCheck 资产盘点
     * @return 资产盘点
     */
    @Override
    public List<AssetsCheck> selectAssetsCheckList(AssetsCheck assetsCheck)
    {
        return assetsCheckMapper.selectAssetsCheckList(assetsCheck);
    }

    /**
     * 新增资产盘点
     * 
     * @param assetsCheck 资产盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetsCheck(AssetsCheck assetsCheck)
    {
        assetsCheck.setCreateTime(DateUtils.getNowDate());
        int rows = assetsCheckMapper.insertAssetsCheck(assetsCheck);
        insertAssetsCheckDetail(assetsCheck);
        return rows;
    }

    /**
     * 修改资产盘点
     * 
     * @param assetsCheck 资产盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetsCheck(AssetsCheck assetsCheck)
    {
        assetsCheckMapper.deleteAssetsCheckDetailByCheckId(assetsCheck.getCheckId());
        insertAssetsCheckDetail(assetsCheck);
        return assetsCheckMapper.updateAssetsCheck(assetsCheck);
    }

    /**
     * 批量删除资产盘点
     * 
     * @param checkIds 需要删除的资产盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsCheckByCheckIds(String checkIds)
    {
        assetsCheckMapper.deleteAssetsCheckDetailByCheckIds(Convert.toStrArray(checkIds));
        return assetsCheckMapper.deleteAssetsCheckByCheckIds(Convert.toStrArray(checkIds));
    }

    /**
     * 删除资产盘点信息
     * 
     * @param checkId 资产盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsCheckByCheckId(Long checkId)
    {
        assetsCheckMapper.deleteAssetsCheckDetailByCheckId(checkId);
        return assetsCheckMapper.deleteAssetsCheckByCheckId(checkId);
    }

    /**
     * 新增资产盘点详情信息
     * 
     * @param assetsCheck 资产盘点对象
     */
    public int insertAssetsCheckDetail(AssetsCheck assetsCheck)
    {
        AssetsCheck newcheck = new AssetsCheck();
        if(assetsCheck.getCheckAssetsType()!=null && !"".equals(assetsCheck.getCheckAssetsType())) {
            newcheck.setCheckAssetsTypeArray(assetsCheck.getCheckAssetsType().split(","));
        }
        if(assetsCheck.getCheckAssetsStatus()!=null && !"".equals(assetsCheck.getCheckAssetsStatus())) {
            newcheck.setCheckAssetsStatusArray(assetsCheck.getCheckAssetsStatus().split(","));
        }
        newcheck.setCheckId(assetsCheck.getCheckId());

        if(assetsCheck.getCheckDepts()!=null && !"".equals(assetsCheck.getCheckDepts())) {
            String getalldepts = getAllDepts(assetsCheck.getCheckDepts());
            System.out.println(getalldepts);
            newcheck.setCheckDeptsArray(getalldepts.split(","));
        }

        //更新盘点资产数
        newcheck.setCheckNum(assetsCheckMapper.getBatchDetailCount(newcheck));
        assetsCheckMapper.updateAssetsCheck(newcheck);

        return assetsCheckMapper.batchAssetsCheckDetail(newcheck);
    }

    public String deptsid = "";
    public String getAllDepts(String checkDepts){
        deptsid+=checkDepts+",";
        //计算所有子部门
        List<SysDept> depts= deptMapper.selectDeptByParentId(Long.parseLong(checkDepts));
        if(depts!=null && !depts.isEmpty()){
            for(int i=0;i<depts.size();i++){
                SysDept dept = depts.get(i);
                getAllDepts(dept.getDeptId()+"");
            }
        }
        return deptsid;
    }


    public int selectAssetsCheckDetailCount(Long checkId,Integer flag){
        Map p = new HashMap<>();
        p.put("checkId",checkId);
        p.put("flag",flag);
        return assetsCheckMapper.selectAssetsCheckDetailCount(p);
    }

    public List<AssetsCheckDetail> selectAssetsCheckDetailList(AssetsCheckDetail assetsCheck){
        return assetsCheckMapper.selectAssetsCheckDetailList(assetsCheck);
    }

    public int checkSave(AssetsCheckDetail assetsCheckDetail){
        long checkDetailId = assetsCheckDetail.getCheckDetailId();
        //盘点是否要报废
        String checkflag = assetsCheckDetail.getCheckFlag();
        if("2".equals(checkflag)){
            //报废资产
            AssetsCheckDetail detailparam = new AssetsCheckDetail();
            detailparam.setCheckDetailId(checkDetailId);
            List<AssetsCheckDetail> l = assetsCheckMapper.selectAssetsCheckDetailList(detailparam);
            if(l!=null && !l.isEmpty()){
                AssetsCheckDetail detail = l.get(0);
                String assetsid = detail.getAssetsId();
                String assetsName = detail.getAssetsName();
                String assetsSN = detail.getAssetsSn();
                AssetsReceive assetsReceive = new AssetsReceive();
                assetsReceive.setAssetsId(assetsid);
                assetsReceive.setAssetsName(assetsName);
                assetsReceive.setAssetsSn(assetsSN);
                assetsReceive.setUserId(assetsCheckDetail.getUserId());
                assetsReceive.setUserName(assetsCheckDetail.getUserName());
                assetsReceive.setReceiveTime(new Date());
                assetsReceive.setReceiveNo("SC" + DateUtils.dateTimeNow());
                assetsReceive.setReceiveType("4");
                assetsReceive.setCreateBy(assetsCheckDetail.getCreateBy());

                List<AssetsInfo> assetsInfoList = new ArrayList<>();
                AssetsInfo info = new AssetsInfo();
                info.setId(assetsid);
                info.setAssetsName(assetsName);
                info.setAssetsSn(assetsSN);
                assetsInfoList.add(info);
                assetsReceive.setAssetsInfoList(assetsInfoList);
                assetsReceiveService.insertAssetsReceiveScrap(assetsReceive);
                assetsCheckDetail.setAssetsStatus("4");
            }
        }else if("3".equals(checkflag)){
            //报修
            AssetsCheckDetail detailparam = new AssetsCheckDetail();
            detailparam.setCheckDetailId(checkDetailId);
            List<AssetsCheckDetail> l = assetsCheckMapper.selectAssetsCheckDetailList(detailparam);
            if(l!=null && !l.isEmpty()) {
                AssetsCheckDetail detail = l.get(0);
                String assetsid = detail.getAssetsId();
                String assetsName = detail.getAssetsName();
                String assetsSN = detail.getAssetsSn();

                AssetsRepairs assetsRepairs = new AssetsRepairs();
                assetsRepairs.setCreateBy(assetsCheckDetail.getCreateBy());
                assetsRepairs.setAssetsId(assetsid);
                assetsRepairs.setRepairsNo("W" + DateUtils.dateTimeNow());
                assetsRepairs.setRepairsTime(new Date());
                assetsRepairs.setUserId(assetsCheckDetail.getUserId());
                assetsRepairs.setUserName(assetsCheckDetail.getUserName());
                assetsRepairs.setUserSeat("盘点报修");
                assetsRepairs.setUserMobile(sysUserService.selectUserById(assetsCheckDetail.getUserId()).getPhonenumber());
                assetsRepairs.setAssetsName(assetsName);
                assetsRepairs.setAssetsSn(assetsSN);
                assetsRepairs.setRepairsNo("盘点报修");
                assetsRepairsService.insertAssetsRepairs(assetsRepairs);
                assetsCheckDetail.setAssetsStatus("3");
            }
        }
        assetsCheckDetail.setCheckFlag("1");
        assetsCheckDetail.setCheckUserId(assetsCheckDetail.getUserId());
        assetsCheckDetail.setCheckUserName(assetsCheckDetail.getUserName());
        assetsCheckDetail.setCheckTime(new Date());
        int result = assetsCheckMapper.updateAssetsCheckDetail(assetsCheckDetail);
        //判断是否全部盘点完成
        int unfinaslnum = selectAssetsCheckDetailCount(assetsCheckDetail.getCheckId(),0);
        if(unfinaslnum==0){
            //设置盘点任务完成
            AssetsCheck assetsCheck = new AssetsCheck();
            assetsCheck.setCheckId(assetsCheckDetail.getCheckId());
            assetsCheck.setCheckStatus("2");
            assetsCheckMapper.updateAssetsCheck(assetsCheck);
        }
        return result;
    }

    public int updateAssetsCheckDetails(AssetsCheckDetail assetsCheckDetail){
        assetsCheckDetail.setAssetsSnArray(assetsCheckDetail.getAssetsSn().split(","));
        int result = assetsCheckMapper.updateAssetsCheckDetails(assetsCheckDetail);
        //判断是否全部盘点完成
        int unfinaslnum = selectAssetsCheckDetailCount(assetsCheckDetail.getCheckId(),0);
        if(unfinaslnum==0){
            //设置盘点任务完成
            AssetsCheck assetsCheck = new AssetsCheck();
            assetsCheck.setCheckId(assetsCheckDetail.getCheckId());
            assetsCheck.setCheckStatus("2");
            assetsCheckMapper.updateAssetsCheck(assetsCheck);
        }
        return result;
    }

    public List<AssetsCheck> selectAssetsCheckDetailsByType(Long checkId){
        return assetsCheckMapper.selectAssetsCheckDetailsByType(checkId);
    }

    public int selectAssetsCheckDetailsByTypeCount(Long checkId, String typename, String isfinash){
        AssetsCheck ac = new AssetsCheck();
        ac.setCheckId(checkId);
        ac.setCheckAssetsType(typename);
        ac.setCheckStatus(isfinash);
        return assetsCheckMapper.selectAssetsCheckDetailsByTypeCount(ac);
    }
}
